package com.danskin.wechat.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.util.StringUtil;
import com.danskin.wechat.entity.CategoryProduct;
import com.danskin.wechat.service.CategoryProductService;
import com.danskin.wechat.util.Page;


@Controller
@RequestMapping("categoryProduct")
public class CategoryProductController extends BaseController {
	@Autowired
	private CategoryProductService categoryProductService;
	
    /**
     * 分类管理List
     * @param categoryProduct
     * @param model
     * @return
     */
	@RequestMapping(value = "queryCategoryProductList", method = RequestMethod.GET)
	public String queryCategoryProductList(CategoryProduct categoryProduct, Model model) {
		return "categoryProduct/categoryProduct_list";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCategoryProductList", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getCategoryProductList(Page page, HttpServletRequest request) throws Exception {
		page.setMap(getRequestParameter(request));
		List<CategoryProduct> listCategoryProduct = categoryProductService.findCategoryProductList(page);
		return pagelistToJSONForBootStrapTable(listCategoryProduct, page);
	}
	/***
	 * 新增分类
	 * @return
	 */
	@RequestMapping(value = "openCategoryProduct",method = RequestMethod.GET)
	public String openCategoryProduct(){
		return "categoryProduct/openCategoryProduct";
	}
	/**
	 * 保存新增分类信息
	 * @param request
	 * @param categoryProduct
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "saveCategoryProduct",method = RequestMethod.POST)
	public String saveCategoryProduct(HttpServletRequest request,CategoryProduct categoryProduct) throws UnsupportedEncodingException{
		CategoryProduct pf = new CategoryProduct();
		pf.setMpId(this.getLoginUser().getMpId());
		pf.setCode("");
		pf.setParentId(0);
		pf.setName(this.getConversionUTF(categoryProduct.getName()));
		String id = String.valueOf(categoryProduct.getId());
		if("null".equals(id)||"0".equals(id)){
			categoryProductService.insertCategoryProduct(pf);
		}else{
			pf.setId(Integer.parseInt(id));
			categoryProductService.updateCategoryProduct(pf);
		}
		return "redirect:/categoryProduct/queryCategoryProductList";
	}
	/***
	 * 修改分类信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editCategoryProduct",method = RequestMethod.GET)
	public String editCategoryProduct(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		CategoryProduct categoryProduct = null;
		if(StringUtil.isNotEmpty(id)){
			categoryProduct = categoryProductService.findCategoryProductById(id);
		}
		model.addAttribute("categoryProduct", categoryProduct);
		return "categoryProduct/updateCategoryProduct";
	}
	/**
	 * 删除分类
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "deleteCategoryProduct", method = RequestMethod.POST)
	public void deleteCategoryProduct(HttpServletRequest request, HttpServletResponse response){
		boolean flag = false;
		String  id = request.getParameter("id");
		if(StringUtil.isNotEmpty(id)){
			categoryProductService.deleteCategoryProduct(id);
			flag = true;
		}
	    renderData(response, flag);
	}
	private void renderData(HttpServletResponse response, boolean data) {
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(data);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}
	//处理中文乱码
  	private static String getConversionUTF(String str) throws UnsupportedEncodingException{
  		if(!"".equals(str)&&str!=null){
  			str = new String(str.getBytes("iso-8859-1"), "UTF-8");
  		}		
  		return str;
  	}
}
